/**
 * 我的消息
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/27
 */
document.addEventListener('plusready' , ready ,  false);

var memberInfo = self = '';
 
function ready(){  
	  
	self = plus.webview.currentWebview();
	memberInfo = plus.storage.getItem('memberInfo'); 
	memberInfo = $.parseJSON(memberInfo);
	
	myMessage();
	
}
 
 
function myMessage(){ 
	var news = plus.storage.getItem('message');
	news = $.parseJSON(news);
	$.each(news.message, function(k , v) {  
		var html = '<div class="background-w margin-bottom-5 table-view table-view-cell order-list" data-msgid="'+v.id+'" data-tid="'+(v.info?v.info.tid:' ')+'" data-id="'+(v.info?v.info.id:' ')+'">';
			html += '<li><span class="color-zx font-13" style="padding:0 5px 0 5px;">'+(v.info?v.info.title:' ')+'</span><span class="mui-pull-right color-c font-13">'+(v.info?v.info.classname:' ')+'</span>';
			if(v.is_read == 0){ 
				html += '<span class="red-circle" data-nwe="true"></span>';
			}
			html += '</li><li class="overflow-h line-h-28"><span class=" font-14 mui-col-xs-9 mui-pull-left">'+v.body+'</span><span class="mui-pull-right color-c font-13 mui-col-xs-3 mui-text-right">'+v.addtime+'</span></li>';
			if(v.reply)html += '<li class="overflow-h line-h-28"><span class=" font-14 mui-col-xs-9 mui-pull-left color-c">回复  '+v.reply+'</span><span class="mui-pull-right color-c font-13 mui-col-xs-3 mui-text-right">'+v.retime+'</span></li></div>';
			$('#content').append(html); 
	})	
	 
	$('.order-list').on('click' , function(){
		var __this = $(this); 
		var tid = __this.attr('data-tid'),id = __this.attr('data-id');	
//		console.log(apiRoot+'/Home/Business/loadDetails/id/'+id)
		if(tid == 33){
			$.ajax({  
				type:'post',
				url:apiRoot+'/Home/Business/loadDetails/',
				data:{
					tid : tid ,
					id : id
				},
				beforeSend:function(){ 
					plus.nativeUI.showWaiting();
				}, 
				complete:function(){ 
					plus.nativeUI.closeWaiting();
				},
				success : function(data){
					var res = eval('('+data+')');
					console.log(JSON.stringify(res))
					if(res){
						var nav_inf = plus.webview.create('nav-inf.html', 'bbs-inf', {}, {
							company_name : res.title,
							level : res.level,
							title : res.title,
							level : res.level,
							aid : res.id,
							tid : res.tid,
							body :res.details,
							address :res.address,
							phone : res.phone,
							contacts : res.contacts,
							number :  res.order_number, 
							good :  res.good_number
						});
						nav_inf.addEventListener('loaded', function (){
							nav_inf.show('pop-in');
							plus.nativeUI.closeWaiting();
						})	
						//更改状态
						updateState(__this.attr('data-msgid') , __this);
					}else{
						toast('获取数据失败')
					}
				},
				error : function(e){
					toast('连接超时 ,请检查网络');
				}
			})
		}else{
			$.ajax({
				type:'post',
				url:apiRoot+'/Home/Article/loadDetails/',
				data:{
					tid : tid ,
					id : id 
				},
				beforeSend:function(){ 
					plus.nativeUI.showWaiting();
				},
				complete:function(){ 
					plus.nativeUI.closeWaiting();
				},
				success : function(data){
					var res = eval('('+data+')');
					if(res){
						var tid = res.tid;
						if(tid == 28){
							var yqhc_inf = plus.webview.create('yqhc-inf.html' , 'yqhc-inf' , {} , { 
								page : self.id,
								aid : res.id , 
								tid : res.tid ,
								pic : res.litpic,
								title : res.title,
								addtime : res.addtime,
								company : res.business.company_name,
								contacts : res.business.contacts,
								phone : res.business.phone,
								body : res.body
		
							});
							yqhc_inf.addEventListener('loaded', function (){
								yqhc_inf.show('pop-in');
								plus.nativeUI.closeWaiting();
							})
						}else if(tid == 29){
							var jsfw_inf = plus.webview.create('jsfw-inf-1.html' , 'jsfw-inf-1' , {} , {
								page : self.id,
								aid : res.id  , 
								tid : res.tid,
								pic : res.litpic,
								title : res.title,
								addtime : res.addtime,
								company : res.business.company_name,
								contacts : res.business.contacts,
								phone : res.business.phone,
								body : res.body
		
							});
							jsfw_inf.addEventListener('loaded', function (){
								jsfw_inf.show('pop-in');
								plus.nativeUI.closeWaiting();
							})
						}else if(tid == 26){
							var hy_inf = plus.webview.create('hy-inf.html' , 'hy-inf' , {} , {
								page : self.id,
								aid : res.id , 
								tid : res.tid, 
								title : res.title,
								body : res.body 
							});
							hy_inf.addEventListener('loaded', function (){
								hy_inf.show('pop-in');
								plus.nativeUI.closeWaiting();
							})
						}else if(tid == 4){ 
							var hd_inf = plus.webview.create('hd-inf.html' , 'hd-inf' , {} , {
								page : self.id,
								tid : res.tid ,
								phone : res.phone, 
								title : res.title,
								body : res.body ,
								aid : res.id
							});
							hd_inf.addEventListener('loaded', function (){
								hd_inf.show('pop-in');
								plus.nativeUI.closeWaiting();
							})	
						}else if(tid == 27){
							var qzzp_inf = plus.webview.create('qzzp-inf.html' , 'qzzp-inf' , {} , { 
								page : self.id,
								aid : res.id , 
								tid : res.tid,
								title : res.title,
								addtime : res.addtime,
								salary : res.salary,
								company : res.company_name,
								education : res.education,
								number :  res.number,
								experience : res.experience,
								contacts : res.contacts,
								phone : res.phone,
								address : res.address,
								body : res.body
							});
							
							qzzp_inf.addEventListener('loaded', function (){
								qzzp_inf.show('pop-in');
								plus.nativeUI.closeWaiting();
							})	
						}else if(tid == 30){
							var bbs_inf = plus.webview.create('bbs-inf.html', 'bbs-inf', {}, {
								page : self.id,
								avatar : res.avatar,
								nickname : res.nickname,
								title : res.title,
								addtime : res.addtime,
								aid : res.id,
								tid : res.tid,
								uid : res.uid,
								body : res.body
							}); 
							bbs_inf.addEventListener('loaded', function (){
								bbs_inf.show('pop-in');
								plus.nativeUI.closeWaiting();
							})	
						}
						//更改状态
						updateState(__this.attr('data-msgid') , __this);
					}else{
						toast('获取不到数据')
					}
	

					
				},
				error : function(e){
					toast('连接超时 ,请检查网络');
				}
			})
		}
		
	})
		
	
}

/**
 * 更新消息的状态
 * @param {Int} id
 */
function updateState(id , __this){
	console.log(id+'--'+__this);
	$.ajax({
		type:'post',
		url : apiRoot+'/Home/Message/updateState/',
		data:{
			id : id
		},
		success:function(data){ 
			if(data == 1){ 
				__this.find('.red-circle').hide();
			}
		},
		error : function(){
			toast('连接超时 ,请检查网络');
		}
	})
	
}

